<?php

/**
 * @copyright   FOSP
 * @version     20120412
 * @todo        Player model
 * @name        Player
 * @author      QuangTM 
 */
class Player
{

    const BASIC_DATA = 'player_basic_data';

    const PLAYER_DETAIL_EURO_2012 = 'player_detail_euro_2012';

    const PLAYER_OF_TEAM_EURO_2012 = 'player_of_team_euro_2012';
    /**
     *
     * @var Player
     */
    protected static $instance;

    protected function __construct()
    {
        
    }

    /**
     * Get Player's instance
     * @return Player
     * @author QuangTM
     */
    public static function getInstance()
    {
        if (!isset(self::$instance) || !self::$instance instanceof self)
            self::$instance = new self;
        return self::$instance;
    }

    public function getBasicDataByIDs($arrPlayerIDs)
    {
        try
        {
            $result = array();
            $arrMissCache = array();
            foreach ($arrPlayerIDs as $playerID)
            {
                $memcacheInstance = Fpt_Global::getMemcacheInstance($playerID);
                $keyCache         = Fpt_Global::makeCacheKey(self::BASIC_DATA, $playerID);
                $playerDetail      = $memcacheInstance->read($keyCache);
                if ($playerDetail === FALSE)
                    array_push ($arrMissCache, $playerID);
                else
                    $result[$playerID] = $playerDetail;
            }
            if(count($arrMissCache))
            {
                // Get application's configuation
                $config = Fpt_Global::getApplicationIni();
                
                // Get mysql object access player
                $playerMysqlObj = Fpt_Business_Player::factory($config['database']['default']);
                
                // Get multi players
                $arrMissPlayers = $playerMysqlObj->getBasicData($arrMissCache);
                
                // Write memcache for each
                foreach ($arrMissPlayers as $playerID => $player)
                {
                    $keyCache = Fpt_Global::makeCacheKey(self::BASIC_DATA, $playerID);
                    Fpt_Global::writeMemcache($player['ID'], $keyCache, $player);
                    $result[$playerID] = $player;
                }
            }
            return $result;
        }
        catch (Exception $ex)
        {
            Fpt_Log::sendlog(4, $ex->getCode(), $ex->getMessage(), $ex->getTraceAsString(), null, null);
            return FALSE;
        }
    }

    public function getDetail($playerId)
    {
        try {
            $playerDetail = array();

            $memcacheInstance = Fpt_Global::getMemcacheInstance($playerId);
            $keyCache         = Fpt_Global::makeCacheKey(self::PLAYER_DETAIL_EURO_2012, $playerId);

            //read cache
            $playerDetail     = $memcacheInstance->read($keyCache);
            
            //miss cache
            if ($playerDetail === false)
            {
                // Get application's configuation
                $config = Fpt_Global::getApplicationIni();

                // Get mysql object access player
                $playerMysqlObj = Fpt_Business_Player::factory($config['database']['default']);

                // Get detail players
                $playerDetail = $playerMysqlObj->getDetail($playerId);

                // write cache
                Fpt_Global::writeMemcache($playerId, $keyCache, $playerDetail);
            }

            return $playerDetail;
        }
        catch (Exception $ex)
        {
            Fpt_Log::sendlog(4, $ex->getCode(), $ex->getMessage(), $ex->getTraceAsString(), null, null);
            return FALSE;
        }
    }

    /**
     * get all player of team with team id
     * @param <int> $teamId
     * @return <array>
     */
    public function getPlayerByTeamId($teamId)
    {
        try {
            $playerOfTeam = array();

            $memcacheInstance = Fpt_Global::getMemcacheInstance($teamId);
            $keyCache         = Fpt_Global::makeCacheKey(self::PLAYER_OF_TEAM_EURO_2012, $teamId);

            //read cache
            $playerOfTeam     = $memcacheInstance->read($keyCache);

            //miss cache
            if ($playerDetail === false)
            {
                // Get application's configuation
                $config = Fpt_Global::getApplicationIni();

                // Get mysql object access player
                $playerMysqlObj = Fpt_Business_Player::factory($config['database']['default']);

                // Get detail players
                $playerOfTeam = $playerMysqlObj->getPlayerByTeamId($teamId);

                // write cache
                Fpt_Global::writeMemcache($teamId, $keyCache, $playerOfTeam);
            }

            return $playerOfTeam;
        }
        catch (Exception $ex)
        {
            Fpt_Log::sendlog(4, $ex->getCode(), $ex->getMessage(), $ex->getTraceAsString(), null, null);
            return FALSE;
        }
    }
}